{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 1、Windows上的倒斜杠(\\\\)以及macOS和Linux上的正斜杠(/)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "- (1) windows的\\\\是Python的转义字符，故表示路径需要\\\\\\\\；但如果加r，则只需要一个\\\\；当然也可以用/\n",
    "  如：\n",
    "  - 'C:\\\\\\\\Users\\\\\\\\17634\\\\\\\\PycharmProjects\\\\\\\\python_basic\\\\\\\\Test'\n",
    "  - r'C:\\Users\\17634\\PycharmProjects\\python_basic\\Test'\n",
    "  - 'C:/Users/17634/PycharmProjects/python_basic/Test'\n",
    "- (2) \\\\和/不要混用，因为macOS和Linux中\\\\不代表路径分隔符，仅仅是一个普通的字符串"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2、操作文件路径的类pathlib.Path"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-06-24T08:45:04.431322600Z",
     "start_time": "2023-06-24T08:45:04.407580600Z"
    }
   },
   "outputs": [],
   "source": [
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "# 1. Path类可以传递多个字段构建一个路径\n",
    "p0 = Path(r\"C:\\Users\\netbian\")\n",
    "p1 = Path('C:/home', 'study', 'python')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-22T12:12:03.884629200Z",
     "start_time": "2023-06-22T12:12:03.842096800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "WindowsPath('/c')"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2. 多个字段中有绝对路径，则该子段前面的字段会被忽略\n",
    "p2 = Path('/a', 'C:/abc', '/d')\n",
    "Path('/a', '/b', '/c')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-22T12:10:56.318858100Z",
     "start_time": "2023-06-22T12:10:56.283442400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "WindowsPath('C:/a/b/c')"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Path('C:/', 'a/b/c')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-21T09:58:48.067096400Z",
     "start_time": "2023-06-21T09:58:48.038098200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "# 3. 利用/进行路径拼接\n",
    "p3 = p1 / 'a' / 'b'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-22T12:12:05.985628100Z",
     "start_time": "2023-06-22T12:12:05.952088400Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3、相对路径和绝对路径"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [],
   "source": [
    "#  从根目录开始写的路径为绝对路径\n",
    "# 1、绝对路径：'C:/home/study/python/a/b'\n",
    "# 2、相对路径之\".\"和\"..\"\n",
    "# \".\"表示当前文件所在的目录，\"..\"表示当前文件所在目录的父目录\n",
    "p4 = Path('.')\n",
    "p5 = Path('./a/temp.py')\n",
    "p6 = Path('..')\n",
    "p7 = Path('../Test/List.py')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-23T10:14:47.067050700Z",
     "start_time": "2023-06-23T10:14:47.037274500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\17634\\PycharmProjects\\automate-the-boring-stuff\\2、读写文件\n",
      "C:\\Users\\17634\\PycharmProjects\\automate-the-boring-stuff\\2、读写文件\\a\\temp.py\n",
      "C:\\Users\\17634\\PycharmProjects\\automate-the-boring-stuff\n",
      "C:\\Users\\17634\\PycharmProjects\\automate-the-boring-stuff\\Test\\List.py\n"
     ]
    }
   ],
   "source": [
    "print(p4.resolve())\n",
    "print(p5.resolve())\n",
    "print(p6.resolve())\n",
    "print(p7.resolve())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-23T10:11:29.712047200Z",
     "start_time": "2023-06-23T10:11:29.654033300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [],
   "source": [
    "# 3. Windows的跟路径是C:或D:，MacOS和Linux的根路径是/"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-21T05:14:59.873779Z",
     "start_time": "2023-06-21T05:14:59.641587Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4、Path对象的常用属性\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "p6 = Path(r'C:\\Users\\17634\\PycharmProjects\\automate-the-boring-stuff\\Test\\List.py')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T08:45:11.191767100Z",
     "start_time": "2023-06-24T08:45:11.168651900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('C:\\\\', 'Users', '17634', 'PycharmProjects', 'automate-the-boring-stuff', 'Test', 'List.py')\n",
      "C:\\Users\\17634\\PycharmProjects\\automate-the-boring-stuff\\Test\n",
      "[WindowsPath('C:/Users/17634/PycharmProjects/automate-the-boring-stuff/Test'), WindowsPath('C:/Users/17634/PycharmProjects/automate-the-boring-stuff'), WindowsPath('C:/Users/17634/PycharmProjects'), WindowsPath('C:/Users/17634'), WindowsPath('C:/Users'), WindowsPath('C:/')]\n",
      "List.py\n",
      ".py\n",
      "List\n"
     ]
    }
   ],
   "source": [
    "print(p6.parts) #把路径分开\n",
    "print(p6.parent) #p6的直接父目录，等价于os.path.dirname(str(p6))\n",
    "print(list(p6.parents)) #p6的所有父目录\n",
    "print(p6.name) #返回当前文件的名字，去掉前面的路径，等价于os.path.basename(str(p6))\n",
    "print(p6.suffix) #返回文件的后缀，等价于os.path.splitext(str(p6))[-1]\n",
    "print(p6.stem) #返回文件名字，去掉后缀"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T08:45:16.516287300Z",
     "start_time": "2023-06-24T08:45:16.479077700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T08:56:23.382958Z",
     "start_time": "2023-06-24T08:56:23.355324200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 5、单个空目录或文件的增删改查"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [],
   "source": [
    "# 1、增加文件\n",
    "p7 = Path('.') / 'temp.py'\n",
    "# (1) #创建一个空的文件\n",
    "p7.touch()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T10:18:22.797113Z",
     "start_time": "2023-06-24T10:18:22.767039800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [],
   "source": [
    "# 新建目录\n",
    "p8 = Path('./temp/a')\n",
    "# parents=True递归创建目录，exist_ok=True表示目录已经存在不会报错\n",
    "p8.mkdir(parents=True, exist_ok=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T10:19:26.326597Z",
     "start_time": "2023-06-24T10:19:26.307597Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [],
   "source": [
    "# 2、删除文件\n",
    "# p7.unlink()\n",
    "#删除空目录\n",
    "p8.rmdir()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T10:17:59.846986800Z",
     "start_time": "2023-06-24T10:17:59.822521700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [],
   "source": [
    "# 3、修改文件\n",
    "# 文件重命名\n",
    "# p7.rename('temp02.py')\n",
    "# 目录重命名(不管目录是否为空都行)\n",
    "(p8 / 'b.txt').touch()\n",
    "p8.rename('./temp/b')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T10:19:55.775133400Z",
     "start_time": "2023-06-24T10:19:55.737508300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "outputs": [],
   "source": [
    "# 4、查询文件\n",
    "p7.exists() #目录和文件通用\n",
    "p7.is_file()\n",
    "p7.is_dir()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T09:34:37.910982500Z",
     "start_time": "2023-06-24T09:34:37.902949500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 6、文件夹遍历"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WindowsPath('../Test/b')\n",
      "WindowsPath('../Test/demo')\n",
      "WindowsPath('../Test/List.py')\n",
      "WindowsPath('../Test/test01.py')\n",
      "WindowsPath('../Test/test02.py')\n",
      "WindowsPath('../Test/test03.py')\n",
      "WindowsPath('../Test/这是测试.txt')\n"
     ]
    }
   ],
   "source": [
    "p9 = Path('../Test')\n",
    "for path in p9.iterdir():\n",
    "    print(repr(path))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T09:45:54.639103300Z",
     "start_time": "2023-06-24T09:45:54.611104300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 7、使用通配符匹配文件"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "outputs": [],
   "source": [
    "# Windows的dir需要/s来表示递归搜索，Linux的find默认是递归搜索\n",
    "# 通配符*代表多个字符，?代表一个字符\n",
    "\n",
    "#glob通配符的pattern\n",
    "\"\"\"\n",
    "*           匹配零或多个任意字符\n",
    "?           匹配任何单个字符\n",
    "[seq]       匹配 seq 中的任何字符\n",
    "[!seq]      匹配任何不在 seq 中的字符\n",
    "\"\"\"\n",
    "# 由于通配符都是贪婪匹配，匹配结构没有正则表达式精确"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-21T06:17:45.531971Z",
     "start_time": "2023-06-21T06:17:45.527410Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "[WindowsPath('../Test/List.py'),\n WindowsPath('../Test/test01.py'),\n WindowsPath('../Test/test02.py'),\n WindowsPath('../Test/test03.py')]"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p9 = Path('../Test')\n",
    "#匹配当前目录下的文件\n",
    "sorted(p9.glob('*.py'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T09:47:54.009264800Z",
     "start_time": "2023-06-24T09:47:53.941380100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "[WindowsPath('../Test/demo/01.py')]"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#匹配当前目录的子目录下的py文件\n",
    "sorted(p9.glob('*/*.py'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T09:47:58.585941700Z",
     "start_time": "2023-06-24T09:47:58.516940400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "[WindowsPath('../Test/demo/01.py'),\n WindowsPath('../Test/List.py'),\n WindowsPath('../Test/test01.py'),\n WindowsPath('../Test/test02.py'),\n WindowsPath('../Test/test03.py')]"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(p9.glob('**/*.py'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T09:48:00.109513900Z",
     "start_time": "2023-06-24T09:47:59.901779300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 注意： 构建的Path对象没有实际访问它，如果对其进行io操作，要确保其存在，不然会报错。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# pathlib模块对应os模块\n",
    "# https://docs.python.org/zh-cn/3/library/pathlib.html#correspondence-to-tools-in-the-os-module"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-06-21T01:54:00.240052Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
